package com.hibernate.ano.test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kevin
 * @Description: 读取注解，并进行相应操作
 * @date 2021/9/6 11:21
 */
public class MyJunitFrameWork {
    public static void main(String[] args) throws Exception {
        Class clazz = EmployeeDAOTest.class;
        Object obj = clazz.newInstance();

        // 获取 EmployeeDAOTest 类中所有的公共方法
        Method[] methods = clazz.getMethods();

        // 迭代出每一个 Method 对象，判断哪些方法上使用了 @MyBefore/ @MyAfter/ @MyTest
        List<Method> myBeforeList = new ArrayList<>();
        List<Method> myAfterList = new ArrayList<>();
        List<Method> myTestList = new ArrayList<>();

        for(Method method : methods) {
            if(method.isAnnotationPresent(MyBefore.class)) {
                myBeforeList.add(method);
            }else if(method.isAnnotationPresent(MyAfter.class)) {
                myAfterList.add(method);
            }else if(method.isAnnotationPresent(MyTest.class)) {
                myTestList.add(method);
            }
        }

        // 执行方法测试
        for(Method method : myTestList) {
            // 先执行 @MyBefore 的方法
            for(Method beforeMethod : myBeforeList) {
                beforeMethod.invoke(obj);
            }

            method.invoke(obj);

            for(Method afterMethod : myAfterList) {
                afterMethod.invoke(obj);
            }
        }
    }
}
